﻿(function () {
    appModule.controller('common.views.users.importExcel', [
        '$scope', '$uibModalInstance', 'abp.services.app.user', 'userId', 'abp.services.app.area', 'user', 'FileUploader', 'abp.services.app.file',
        function ($scope, $uibModalInstance, userService, userId, areaService, user, fileUploader, fileService) {
            var vm = this;
            vm.saving = false;
            vm.user = null;
            vm.profilePictureId = null;
            vm.roles = [];
            vm.setRandomPassword = false;
            vm.sendActivationEmail = false;
            vm.canChangeUserName = true;
            vm.isTwoFactorEnabled = abp.setting.getBoolean("Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled");
            vm.isLockoutEnabled = abp.setting.getBoolean("Abp.Zero.UserManagement.UserLockOut.IsEnabled");

            vm.fileClick = function () {
                $("#FileID").click();
            };

            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //触发保存方法
            vm.save = function () {

            };

            //上传方法
            vm.uploader = new fileUploader({
                url: abp.appPath + 'Profile/UploadUserExcel',
                headers: {
                    "X-XSRF-TOKEN": abp.security.antiForgery.getToken()
                },
                queueLimit: 1,
                autoUpload: true,
                removeAfterUpload: true,
                filters: [{
                    name: 'userExcel',
                    fn: function (item, options) {
                        //File size check
                        if (item.size > 4194304) //4MB
                        {
                            abp.message.warn(app.localize('File_Warn_FileType'));
                            return false;
                        }

                        return true;
                    }
                }]
            });

            vm.uploader.onSuccessItem = function (fileItem, response, status, headers) {
                console.log(response)
                vm.cancel();
                if (response.success) {
                    abp.message.success('成功插入' + response.result.data.successUsers.length + '条数据！', '提示');
                } else {
                    abp.message.error(app.localize(response.error.message), '提示');
                }
            };

            function init() {
                userService.getUserForEdit({
                    id: userId
                }).then(function (result) {
                    vm.user = result.data.user;
                    vm.profilePictureId = result.data.profilePictureId;
                    vm.user.passwordRepeat = vm.user.password;
                    vm.roles = result.data.roles;
                    vm.canChangeUserName = vm.user.userName != app.consts.userManagement.defaultAdminUserName;
                    vm.user.shouldChangePasswordOnNextLogin = false;
                    //vm.user.userType = "非群众";
                });
            }

            init();
        }
    ]);
})();